package com.go.ssh.point.web.pub.action;

import com.go.ssh.commons.Constants;
import com.go.ssh.commons.web.action.BaseAction;
import com.go.ssh.point.exception.UserExistsException;
import com.go.ssh.point.exception.UserNotFoundException;
import com.go.ssh.point.model.UserModel;
import com.go.ssh.point.service.IAccountService;
import com.go.ssh.point.web.account.vo.AccountInfo;
import com.opensymphony.xwork2.Action;

@SuppressWarnings("serial")
public class LoginAction extends BaseAction {
	private UserModel user;
	private IAccountService userService;
	private AccountInfo userVO;

	public String login() {
		return Action.LOGIN;
	}

	public String doLogin() {
		try {
			user = userService.login(user);
			getActionContext().getSession().put(
					Constants.USER_INFO_SESSION_KEY, user);
		} catch (UserNotFoundException e) {
			return Action.LOGIN;
		}
		return Action.SUCCESS;
	}

	public String register() {
		return "register";
	}

	public String doRegister() {
		UserModel user = new UserModel();
		user.setUsername(userVO.getUsername());
		user.setPassword(userVO.getPassword());

		try {
			userService.register(user);
		} catch (UserExistsException e) {
			return LOGIN;
		}

		return SUCCESS;
	}

	public UserModel getUser() {
		return user;
	}

	public void setUser(UserModel user) {
		this.user = user;
	}

	public IAccountService getUserService() {
		return userService;
	}

	public void setUserService(IAccountService userService) {
		this.userService = userService;
	}

	public AccountInfo getUserVO() {
		return userVO;
	}

	public void setUserVO(AccountInfo userVO) {
		this.userVO = userVO;
	}
}
